using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnowballGameZone : MonoBehaviour
{
	private string snowBallKey = "Snowball";

	private int minCount = 5;

	private string currentWeaponName;

	private string currentSlotName;

	[SerializeField]
	private Vector2 leftTop;

	[SerializeField]
	private Vector2 rightDown;

	[SerializeField]
	private List<string> enableWeapon;

	private bool hasEntered;

	private void Awake()
	{
		if (settings.offlineMode || GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.tanks || GameController.instance.curTypeGame == TypeGame.TDM)
		{
			base.gameObject.SetActive(false);
		}
	}

	private void Update()
	{
		if (Time.frameCount % 10 != 0)
		{
			return;
		}
		PlayerBehavior myPlayer = PlayerBehavior.MyPlayer;
		if (!(myPlayer != null))
		{
			return;
		}
		Vector3 position = myPlayer.transform.position;
		bool flag = ((position.x >= leftTop.x && position.x <= rightDown.x) || (position.x >= rightDown.x && position.x <= leftTop.x)) && ((position.z >= leftTop.y && position.z <= rightDown.y) || (position.z >= rightDown.y && position.z <= leftTop.y));
		if (flag != hasEntered)
		{
			if (flag)
			{
				EnterInGameZone();
			}
			else
			{
				ExitFromGameZone();
			}
		}
	}

	private void EnterInGameZone()
	{
		PlayerBehavior myPlayer = PlayerBehavior.MyPlayer;
		if (hasEntered)
		{
			return;
		}
		int consumableCount = settings.GetConsumableCount(snowBallKey);
		if (consumableCount < minCount)
		{
			MarketController.Instance.BuyProduct(snowBallKey, true, minCount - consumableCount);
			MarketController.Instance.ChangeConsumableCount(snowBallKey);
		}
		currentWeaponName = myPlayer.weaponManager.currentWeaponScript.name;
		currentSlotName = myPlayer.weaponManager.currentWeaponSlotName;
		int num = -1;
		for (int i = 0; i < enableWeapon.Count; i++)
		{
			Product product = MarketController.Instance.GetProduct(enableWeapon[i]);
			if (product.id.Equals("Snowball"))
			{
				num = i;
			}
			if (product.Purchased && currentWeaponName == enableWeapon[i])
			{
				MarketController.Instance.SelectWeaponSlot(product.subCategoryName.ToLower());
				break;
			}
			if (num != -1)
			{
				MarketController.Instance.SelectWeaponSlot(MarketController.Instance.GetProduct(enableWeapon[num]).subCategoryName.ToLower());
			}
		}
		myPlayer.isImmortal = true;
		myPlayer.weaponManager.RemoveAllWeapon();
		myPlayer.weaponManager.SetEnableWeapon(enableWeapon);
		myPlayer.DisableChangeWeapon();
		myPlayer.DeactivateAllChargesOnline();
		StartCoroutine(SetNewWeapon());
		hasEntered = true;
	}

	private IEnumerator SetNewWeapon()
	{
		yield return new WaitForSeconds(0.1f);
		int indexSnowball = -1;
		for (int i = 0; i < enableWeapon.Count; i++)
		{
			Product product = MarketController.Instance.GetProduct(enableWeapon[i]);
			if (product.id.Equals("Snowball"))
			{
				indexSnowball = i;
			}
			if (product.Purchased && currentWeaponName == enableWeapon[i])
			{
				MarketController.Instance.WeaponEquipped(product.subCategoryName.ToLower(), enableWeapon[i]);
				MarketController.Instance.SelectWeaponSlot(product.subCategoryName.ToLower());
				break;
			}
			if (indexSnowball != -1)
			{
				Product snowball = MarketController.Instance.GetProduct(enableWeapon[indexSnowball]);
				MarketController.Instance.WeaponEquipped(snowball.subCategoryName.ToLower(), enableWeapon[indexSnowball]);
				MarketController.Instance.SelectWeaponSlot(snowball.subCategoryName.ToLower());
			}
		}
		MarketController.Instance.SetSlotNotSelected();
	}

	private void ExitFromGameZone()
	{
		PlayerBehavior myPlayer = PlayerBehavior.MyPlayer;
		if (hasEntered)
		{
			myPlayer.EnableChangeWeapon();
			myPlayer.isImmortal = false;
			MarketController.Instance.SendEquipedWeapon();
			MarketController.Instance.SelectWeaponSlot(currentSlotName);
			MarketController.Instance.SelectedWeaponSlotIfSelected();
			hasEntered = false;
		}
	}
}
